package cn.ac.istic.lkt.atv;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.util.ArrayList;
import java.util.List;



public class TreePanel2String extends TreePanelAbstract {
	private static final long serialVersionUID = -2588318327209259315L;
	private ParseTree pt1 = null;
	private String[] sent= null;
	private float[] wordX = null;
	private float[] wordmidX = null;
	private float wordY=0;
	private ArrayList<Align> align = null;
	private boolean directionfe = true;

	public class Align{
		public int f;
		public int e;
		public Align(int x,int y){
			e=x;
			f=y;
		}
	}
	
	public TreePanel2String(){
		super();
	}
	
	public void init(ParseTree pt1, String sentence, String a, String d){
		this.pt1 = pt1;
		this.sent = sentence.split(" ");
		wordX = new float[this.sent.length];
		wordmidX = new float[this.sent.length];
		this.setDirection(d);
		buildAlign(a);
	}
	
	public void buildAlign(String s) {
		try {
			String[] a = s.trim().split(" ");
			align = new ArrayList<Align>(a.length);
			for (int i = 0; i < a.length; i++) {
				String[] b = a[i].split("-");
				int e = Integer.parseInt(b[0]);
				int f = Integer.parseInt(b[1]);
				//System.err.println(""+e+" "+f+" "+this.pt1.getWordCount()+" "+this.pt2.getWordCount());
				if((directionfe && (e>=this.sent.length || f>=this.pt1.getWordCount())) 
					|| (!directionfe && (e>=this.pt1.getWordCount() || f>=this.sent.length))){
					System.err.println("Alignment out of range, discard it.");
					this.align = null;
					return;
				}
				align.add(new Align(e,f));
			}
			
		} catch (Exception e) {
			System.err.println("Parsing alignment error: " + s);
			align = null;
		}
	}
	public void myPaint(Graphics2D g2) {
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);		
		
		g2.setFont(TFONT);
		//System.out.println("myPaintmyPaintmyPaintmyPaintmyPaintmyPaint");
		FontRenderContext frc = g2.getFontRenderContext();
		charHeight = (float) TFONT.getMaxCharBounds(frc).getHeight();
		charWidth = ((float) TFONT.getMaxCharBounds(frc).getWidth());

		if (this.pt1 == null || this.sent == null) {
			g2.drawString("trees", 10, 20);
			return;
		}

		calcPos(frc);
		calcSentPos(frc);
		g2.setPaint(Color.white);
		g2.fillRect(0, 0, imgWidth, imgHeight);
		g2.setPaint(Color.black);
		List<PTNode> nodelist1 = pt1.getNodeList();
		for (int i = 0; i < nodelist1.size(); i++) {
			PTNode nd = nodelist1.get(i);
			if (nd.isWord()) {
				g2.setFont(TFONT);
			} else
				g2.setFont(TFONT);
			g2.drawString(nd.getLabel(), nd.getX(), nd.getY());
		}
		
		for (int i = 0; i < this.sent.length; i++) {		
			//System.err.println("draw string: "+sent[i]+" "+wordX[i]+" "+wordY);
			
			g2.drawString(sent[i], wordX[i], wordY);
		}

		drawLines(g2, pt1, nodelist1.get(0));		
		if(this.align != null) drawAlign(g2);
	}
	public void drawAlign(Graphics2D g2){
		//Vector<Integer> wpf=pt1.getWordPos();		
		List<PTNode> ndf = pt1.getNodeList();		
		for (int i=0; i< align.size(); i++){
			Align a = align.get(i);
			PTNode tnf;
			Shape line;
			if(this.directionfe){
				//System.err.println("fe: "+a.f+" "+a.e);
				tnf = ndf.get(pt1.getWordPos().get(a.f));				
				line = new java.awt.geom.Line2D.Double( tnf.getXmiddle(),
						tnf.getYdown(), wordmidX[a.e], wordY- charHeight);
			}
			else{
				//System.err.println("ef: "+a.f+" "+a.e);
				tnf = ndf.get(pt1.getWordPos().get(a.e));
				line = new java.awt.geom.Line2D.Double( tnf.getXmiddle(),
						tnf.getYdown(), wordmidX[a.f], wordY - charHeight);
			}
			g2.setColor(Color.red);
			g2.draw(line);
		}
		
	}
	public void calcPos(FontRenderContext frc) {
		pt1.setX(frc);// 设置每一个树节点的X坐标		
		pt1.setY(frc); // 设置每一个树节点的Y坐标
		
		if (pt1.getMaxX() > this.wordX[this.wordX.length-1]) {
			imgWidth = (int) (pt1.getMaxX() + STARTX);
		} else
			imgWidth = (int) (this.wordX[this.wordX.length-1] + STARTX);
		imgHeight = (int) (pt1.getMaxY() + 4*STARTX);
		//System.out.println("TreePanel2String: "+imgWidth+" "+imgHeight);
		
	}
	
	public void calcSentPos(FontRenderContext frc){
		this.wordY = pt1.getMaxY()+TOPY*3;
		StringBuffer sb = new StringBuffer();
		this.wordX[0] = STARTX;
		float strw, lastp;
		strw = (float)TreePanelAbstract.TFONT.getStringBounds(sent[0], frc).getWidth();
		lastp = STARTX+strw;
		this.wordmidX[0] = STARTX+ strw/2;
		for (int i=1; i<this.sent.length; i++){
			this.wordX[i] = lastp + wordSpace;
			strw = (float)TreePanelAbstract.TFONT.getStringBounds(sent[i], frc).getWidth();
			lastp = this.wordX[i]+strw;
			this.wordmidX[i] = this.wordX[i] + strw/2;
			sb.append(sent[i]);
		}		
		
		if (lastp<pt1.getMaxX()){
			float intval = (pt1.getMaxX()-(float)TreePanelAbstract.TFONT.getStringBounds(
					sb.toString(),frc).getWidth())/(sent.length);
			lastp = STARTX+(float)TreePanelAbstract.TFONT.getStringBounds(sent[0], frc).getWidth();
			for (int i=1; i<this.sent.length; i++){
				this.wordX[i] = lastp + intval;
				strw = (float)TreePanelAbstract.TFONT.getStringBounds(sent[i], frc).getWidth();
				lastp = this.wordX[i]+strw;
				this.wordmidX[i] = this.wordX[i] + strw/2;
				sb.append(sent[i]);
			}	
		}
		
	}
	
	public void setDirection(String d){
		if (d.equals("fe")){
			this.directionfe = true;
		}
		else if (d.equals("ef")){
			this.directionfe = false;
		}
		else{
			System.err.println("Wrong alignment direction, using default: f-e");
		}
	}
}

